package com.sfddata.musicpillow.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfddata.musicpillow.user.entity.CouldHaveReportDo;
import com.sfddata.musicpillow.user.entity.MonthList;
import com.sfddata.musicpillow.user.entity.MonthReportInfo;
import com.sfddata.musicpillow.user.entity.TbSleepRecordEntity;
import com.sfddata.musicpillow.user.service.MonthReportInfoService;
import com.sfddata.musicpillow.user.mapper.MonthReportInfoMapper;
import com.sfddata.musicpillow.user.service.TbSleepRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author jiangyingou
* @description 针对表【month_report_info】的数据库操作Service实现
* @createDate 2023-06-16 15:46:32
*/
@Service
public class MonthReportInfoServiceImpl extends ServiceImpl<MonthReportInfoMapper, MonthReportInfo>
    implements MonthReportInfoService{

    @Autowired
    TbSleepRecordService tbSleepRecordService;

    @Autowired
    MonthReportInfoMapper monthReportInfoMapper;

    @Override
    public void getMonthReportInfo(MonthReportInfo monthReportInfo){

        QueryWrapper<MonthReportInfo> monthReportInfoQueryWrapper = new QueryWrapper<>();
        monthReportInfoQueryWrapper.eq("user_id",1);
        MonthReportInfo monthReportInfo1 = this.getOne(monthReportInfoQueryWrapper);
        System.out.println(monthReportInfo1);
    }

    @Override
    public MonthList getByUserId(Long id) {

        MonthList monthList = new MonthList();

        QueryWrapper<TbSleepRecordEntity> dayqw = new QueryWrapper<>();
        dayqw.eq("col_user_id", id);
        List<TbSleepRecordEntity> list1 = tbSleepRecordService.list(dayqw);

        // 如果本月日报满7天但是没有这个月的月报就是未读，如果有就是已读
        QueryWrapper<MonthReportInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<MonthReportInfo> list = this.list(queryWrapper);
        HashSet<String> set = new HashSet<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
        Calendar cal = Calendar.getInstance();
        // 这个只是已读列表
        if (list.size() > 0){
            for (MonthReportInfo entity : list) {
                cal.setTime(entity.getStartDay());
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH) + 1;
                if (month<10){
                    set.add(year +"-0"+ month);
                }else{
                    set.add(year +"-"+ month);
                }
            }
        }
        List<String> datelist = set.stream().collect(Collectors.toList());

        // 未读列表
        List<String> haveReport = monthReportInfoMapper.selectMonthHaveReport(id.intValue());
        List<CouldHaveReportDo> couldHaveReportDos = monthReportInfoMapper.selectMonthCouldHaveReport(id.intValue());
        // 满足条件的月份
        List<String> collect = couldHaveReportDos.stream().map(CouldHaveReportDo::getMon).collect(Collectors.toList());

        List<String> unRead = collect.stream().filter(
                item -> !haveReport.contains(item)
        ).collect(Collectors.toList());

        monthList.setHaveReport(datelist);
        monthList.setUnRead(unRead);
        return monthList;
    }


}




